package monster.com.cvh.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.Permission;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import monster.com.cvh.R;
import monster.com.cvh.activity.base.PermissionActivity;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.SPUtils;

/**
 * Created by ASUS on 2018/2/7.
 * 引导页
 */

public class GuideActivity extends PermissionActivity {
    @BindView(R.id.vp_activity_welcome_guide)
    ViewPager mViewPager;
    @BindView(R.id.ll_dots_layout)
    LinearLayout mLlDotsWrappert;
    @BindView(R.id.tv_guide_emjoy)
    TextView mTvEmjoy;

    private ImageView[] mDotList;
    private int mLastPostion;
    private List<View> mViewList;
    private MyViewPagerAdapter mAdapter;


    @Override
    protected int getContentViewResId() {
        //全屏显示
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        //适配刘海
        if (Build.VERSION.SDK_INT >= 28) {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            getWindow().setAttributes(lp);
        }
        return R.layout.activity_guide;
    }

    @Override
    public void initEvent() {
        mAdapter = new MyViewPagerAdapter(mViewList, this);
        mViewPager.setAdapter(mAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position != mDotList.length - 1) {
                    setCurrentDotPosition(position);
                    mTvEmjoy.setVisibility(View.GONE);
                    mLlDotsWrappert.setVisibility(View.VISIBLE);
                } else {
                    mLlDotsWrappert.setVisibility(View.GONE);
                    mTvEmjoy.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mLastPostion = 0;
        mDotList[0].setEnabled(true);


    }

    @Override
    public void initData() {
        mViewList = new ArrayList<>();
        LayoutInflater inflater = LayoutInflater.from(GuideActivity.this);
        mViewList.add(inflater.inflate(R.layout.guide_one_layout, null));
        mViewList.add(inflater.inflate(R.layout.guide_two_layout, null));
        mViewList.add(inflater.inflate(R.layout.guide_three_layout, null));
        mDotList = new ImageView[mViewList.size()];
        for (int i = 0; i < mViewList.size(); i++) {
            mDotList[i] = (ImageView) mLlDotsWrappert.getChildAt(i);
            mDotList[i].setEnabled(false);
        }

    }

    private void setCurrentDotPosition(int position) {
        mDotList[position].setEnabled(true);
        mDotList[mLastPostion].setEnabled(false);
        mLastPostion = position;
    }

    private void goLoginActivity() {
        SPUtils.putBoolean(GuideActivity.this, MyConstant.IS_FIRST_IN, false);
        startActivity(new Intent(GuideActivity.this, LoginActivity.class));
        finish();
    }

    @OnClick(R.id.tv_guide_emjoy)
    public void onViewClicked() {
        requestPermission( new Action() {
            @Override
            public void onAction(Object data) {
                goLoginActivity();
            }
        }, null, Permission.WRITE_EXTERNAL_STORAGE);
    }

    private class MyViewPagerAdapter extends PagerAdapter {
        private List<View> mViewList = null;

        private Context mContext;

        public MyViewPagerAdapter(List<View> mViewList, Context context) {
            this.mViewList = mViewList;
            this.mContext = context;
        }

        @Override
        public int getCount() {
            return mViewList.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (mViewList != null) {
                if (mViewList.size() > 0) {
                    container.addView(mViewList.get(position));
                    return mViewList.get(position);
                }

            }
            return null;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            if (mViewList != null) {
                if (mViewList.size() > 0) {
                    container.removeView(mViewList.get(position));
                }
            }
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}
